import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { CacheUserUtil } from '@/assets/js/utils/CacheUserUtil'

const routes: Array<RouteRecordRaw> = [
    {
        // 登录页
        path: '/',
        name: 'Login',
        component: () => import('@/views/login/index.vue')
    },
    {
        // 首页
        path: '/portal',
        //组件名称
        name: 'Portal',
        // 引入组件，组件Home.vue所在路径
        // Home.vue是需要路由的vue组件
        component: () => import('@/views/portal/index.vue'),
        children: [
            {
                // 工作台
                path: 'workPlace',
                name: 'WorkPlace',
                component: () => import('@/views/workPlace/index.vue')
            },
            {
                // 日程待办
                path: 'todo',
                name: 'Todo',
                component: () => import('@/views/todo/index.vue')
            },
            {
                // 应用中心
                path: 'app',
                name: 'App',
                children: [
                    {
                        // 组织
                        path: 'organization',
                        name: 'Organization',
                        children: [
                            {
                                // 通讯录
                                path: 'mailList',
                                name: 'MailList',
                                component: () => import('@/views/organization/mailList/index.vue')
                            },
                            {
                                // 组织管理
                                path: 'management',
                                name: 'Management',
                                component: () => import('@/views/organization/orgManagement/index.vue')
                            }
                        ]
                    },
                    {
                        // 配置
                        path: 'settings',
                        name: 'Settings',
                        children: [
                            {
                                // 菜单管理
                                path: 'menuManagement',
                                name: 'MenuManagement',
                                component: () => import('@/views/settings/menuManagement/index.vue')
                            }
                        ]
                    }
                ]
            }
        ]
    }

]

const router = createRouter({
    routes,
    history: createWebHistory(import.meta.env.BASE_URL)
})

// 定义白名单
const whileList = [
    '/'
]
// 路由前置守卫
router.beforeEach((to, from, next) => {
    const token = CacheUserUtil.getToken()
    if (whileList.includes(to.path) || token) {
        // 如果是登录页，则跳转到首页
        if (to.path === '/' && token) {
            console.log(`已经登录，无法前往登录页，token: ${token}`)
            next('/portal')
        } else {
            next()
        }
    } else {
        // 跳转到登录页
        next('/')
    }
})

export default router